---@class Battle.PlayerRobot
local PlayerRobot = Battle.CreateClass("PlayerRobot")
Battle.PlayerRobot = PlayerRobot
function PlayerRobot:Init(playerId,camp,cards)
    self.playerId = playerId
    self.camp = camp
    ---@type Battle.Card[]
    self.cards = cards

    ---@type Battle.Card[]
    self.handCards = {}
    self.totalWeight = 0
    for k,v in pairs(self.cards) do
        self.totalWeight = self.totalWeight + v.weight
    end
end
function PlayerRobot:OnInitialize()
    self:InitHandCard()
    self:InitEnergy()
    self:InitAction()
    self:OnEnergyUpdateFinished()
    self:OnActionUpdateFinished()
end
function PlayerRobot:InitEnergy()

    local Global = Battle.GlobalConfig
    ---能量值
    self.energy = Global.GetInt(2)
    self.energyMax = Global.GetInt(1)
    self.energyRecoveryTime = Global.GetInt(3)/1000
    self.energyTime = 0
end
function PlayerRobot:InitAction()
    local Global = Battle.GlobalConfig
    ---行动点
    self.action = Global.GetInt(4)
    self.actionMax = Global.GetInt(5)
    self.actionCost = Global.GetInt(6)
    local recovery = decodeJsonStrToTable(Global.GetString(7))
    self.actionRecoveryTime = 1---恢复时间
    self.actionRecoveryCount = math.floor(recovery[2] * 1/(recovery[1]/1000)) ---每次恢复多少
    self.actionTime = 0
end

function PlayerRobot:Update(dt)
    if Battle.ClientBattle.battleStage < Battle.BattleStage.Run then
        return
    end
    if self.energy < self.energyMax then
        self.energyTime = self.energyTime - dt
        if self.energyTime <= 0 then
            self.energy = self.energy + 1
            self.energyTime = self.energyRecoveryTime
            self:OnEnergyUpdateFinished()
        end
    end
    if self.action < self.actionMax then
        self.actionTime = self.actionTime - dt
        self.action = self.action + self.actionRecoveryCount * dt
        if self.actionTime <= 0 or self.action >= self.actionMax then
            self.action = math.min(self.action,self.actionMax)
            self.actionTime = self.actionRecoveryTime
            self:OnActionUpdateFinished()
        end
    end
end
function PlayerRobot:ChangeEnergyRecover(energyRecoveryTime)
    self.energyRecoveryTime = energyRecoveryTime
    self:OnEnergyUpdateFinished()
end
function PlayerRobot:CreateCardEntity(id,posX,posY,dirX)
    if Battle.ClientBattle:IsBattleFinished() then
        return
    end
    Battle.EntityFactory.CreateHero(id,self.camp,posX,0,posY,dirX or -1,0,0)
end
function PlayerRobot:UseHandCard(index)
    if Battle.ClientBattle:IsBattleFinished() then
        return
    end
    if index > #self.handCards then
        logError("UseHandCard index 有误 "..index)
        return
    end
    local card = self.handCards[index]
    if self.energy < card.cost then
        return
    end
    table.remove(self.handCards,index)
    table.insert(self.handCards,self:RandomCard())
    self.energy = self.energy - card.cost
    self:OnEnergyUpdateFinished()
    self:OnUseHandCardFinished(card)
end
---@param entity Battle.Entity
function PlayerRobot:ReduceAction(entity)
    local action = self.action - self.actionCost*(1+entity.attrSystem:GetAttr(Battle.AttributeType.ACTION_COST_PER)/10000)
    if action < 0 then
        return false
    end
    self.action = action
    self:OnActionUpdateFinished()
    return true
end

function PlayerRobot:InitHandCard()
    for i = 1,5 do
        table.insert(self.handCards,self:RandomCard())
    end
end
function PlayerRobot:IsCardInHand(heroId)
    for k,v in ipairs(self.handCards) do
        if v.heroId == heroId then
            return true
        end
    end
    return false
end

---@return Battle.Card
function PlayerRobot:RandomCard()
    local weight = 0
    local list = TablePool.Get()
    for k,v in ipairs(self.cards) do
        if not self:IsCardInHand(v.heroId) then
            table.insert(list,v)
            weight = weight + v.weight
        end
    end
    if #list > 0 then
        local w = math.random(0,weight)
        for k,v in ipairs(list) do
            if w <= v.weight then
                return v
            end
            w = w - v.weight
        end
    end
    TablePool.Release(list)
    weight = math.random(0,self.totalWeight)
    for k,v in ipairs(self.cards) do
        if weight <= v.weight then
            return v
        end
        weight = weight - v.weight
    end
    return self.cards[1]
end

function PlayerRobot:OnEnergyUpdateFinished()

end
function PlayerRobot:OnActionUpdateFinished()

end
function PlayerRobot:OnUseHandCardFinished(card)

end
function PlayerRobot:OnRandomCardBefore()

end